#!/bin/bash

# Android rom create(fully create) shell of Sony Xperia XZ1 Dual G8342.
# Please put this file into the sub directory.
#   e.g: [Your android source code root directory]/[sub directory named 'shell']/sony_poplar_dsds_deploy_shell
# 
# You should change directory and execute some command to create dependency projects.
#   e.g: 
#       cd [Your android source code root directory]/[sub directory named 'shell']/sony_poplar_dsds_deploy_shell
#       sh sony_poplar_dsds_create_fully.sh
# 
# create by: SteveJrong
# version: 1.0

ROOT_PATH=
getRootPath(){
	ORIGINAL_PATH=$(pwd)
	SUB_PATH=${ORIGINAL_PATH%/*}
	ROOT_PATH=${SUB_PATH%/*}
}
getRootPath

VENDOR_NAME=sony
DEVICE_ROOT_PATH=$ROOT_PATH/device
KERNEL_ROOT_PATH=$ROOT_PATH/kernel
VENDOR_ROOT_PATH=$ROOT_PATH/vendor

# android device
ANDROID_DEVICE_SONY_POPLAR_DSDS_URL=https://gitee.com/sony_xperia_xz1/android_device_sony_poplar_dsds.git
ANDROID_DEVICE_SONY_POPLAR_DSDS_BRANCH_NAME=android-12.1
ANDROID_DEVICE_SONY_POPLAR_DSDS_TRULY_PROJECT_NAME=poplar_dsds
ANDROID_DEVICE_SONY_POPLAR_DSDS_PROJECT_PATH=$DEVICE_ROOT_PATH/$VENDOR_NAME/$ANDROID_DEVICE_SONY_POPLAR_DSDS_TRULY_PROJECT_NAME # device/sony/poplar_dsds

ANDROID_DEVICE_SONY_YOSHINO_COMMON_URL=https://gitee.com/sony_xperia_xz1/android_device_sony_yoshino-common.git
ANDROID_DEVICE_SONY_YOSHINO_COMMON_BRANCH_NAME=android-12.1
ANDROID_DEVICE_SONY_YOSHINO_COMMON_TRULY_PROJECT_NAME=yoshino-common
ANDROID_DEVICE_SONY_YOSHINO_COMMON_PROJECT_PATH=$DEVICE_ROOT_PATH/$VENDOR_NAME/$ANDROID_DEVICE_SONY_YOSHINO_COMMON_TRULY_PROJECT_NAME # device/sony/yoshino-common

# android kernel
ANDROID_KERNEL_SONY_MSM8998_URL=https://gitee.com/sony_xperia_xz1/android_kernel_sony_msm8998.git
ANDROID_KERNEL_SONY_MSM8998_BRANCH_NAME=android-12.1
ANDROID_KERNEL_SONY_MSM8998_TRULY_PROJECT_NAME=msm8998
ANDROID_KERNEL_SONY_MSM8998_PROJECT_PATH=$KERNEL_ROOT_PATH/$VENDOR_NAME/$ANDROID_KERNEL_SONY_MSM8998_TRULY_PROJECT_NAME # kernel/sony/msm8998

# android vendor
ANDROID_VENDOR_SONY_POPLAR_DSDS_URL=https://gitee.com/sony_xperia_xz1/android_vendor_sony_poplar_dsds.git
ANDROID_VENDOR_SONY_POPLAR_DSDS_BRANCH_NAME=android-12.1
ANDROID_VENDOR_SONY_POPLAR_DSDS_TRULY_PROJECT_NAME=poplar_dsds
ANDROID_VENDOR_SONY_POPLAR_DSDS_PROJECT_PATH=$VENDOR_ROOT_PATH/$VENDOR_NAME/$ANDROID_VENDOR_SONY_POPLAR_DSDS_TRULY_PROJECT_NAME # vendor/sony/poplar_dsds

ANDROID_VENDOR_SONY_YOSHINO_COMMON_URL=https://gitee.com/sony_xperia_xz1/android_vendor_sony_yoshino-common.git
ANDROID_VENDOR_SONY_YOSHINO_COMMON_BRANCH_NAME=android-12.1
ANDROID_VENDOR_SONY_YOSHINO_COMMON_TRULY_PROJECT_NAME=yoshino-common
ANDROID_VENDOR_SONY_YOSHINO_COMMON_PROJECT_PATH=$VENDOR_ROOT_PATH/$VENDOR_NAME/$ANDROID_VENDOR_SONY_YOSHINO_COMMON_TRULY_PROJECT_NAME # vendor/sony/yoshino-common

# clear directory
echo -e "··············· Preparing to clean the directory.\n"
## clear android device
echo -e "··············· Preparing to clean the android device directory.\n"
if [ -d $ANDROID_DEVICE_SONY_POPLAR_DSDS_PROJECT_PATH ]; then
    rm -rf $ANDROID_DEVICE_SONY_POPLAR_DSDS_PROJECT_PATH
    echo -e "··············· Done!\n"
fi
if [ -d $ANDROID_DEVICE_SONY_YOSHINO_COMMON_PROJECT_PATH ]; then
    rm -rf $ANDROID_DEVICE_SONY_YOSHINO_COMMON_PROJECT_PATH
    echo -e "··············· Done!\n"
fi
## clear android kernel
echo -e "··············· Preparing to clean the android kernel directory.\n"
if [ -d $ANDROID_KERNEL_SONY_MSM8998_PROJECT_PATH ]; then
    rm -rf $ANDROID_KERNEL_SONY_MSM8998_PROJECT_PATH
    echo -e "··············· Done!\n"
fi
## clear android vendor
echo -e "··············· Preparing to clean the android vendor directory.\n"
if [ -d $ANDROID_VENDOR_SONY_POPLAR_DSDS_PROJECT_PATH ]; then
    rm -rf $ANDROID_VENDOR_SONY_POPLAR_DSDS_PROJECT_PATH
    echo -e "··············· Done!\n"
fi
echo -e "··············· Preparing to clean the android vendor directory.\n"
if [ -d $ANDROID_VENDOR_SONY_YOSHINO_COMMON_PROJECT_PATH ]; then
    rm -rf $ANDROID_VENDOR_SONY_YOSHINO_COMMON_PROJECT_PATH
    echo -e "··············· Done!\n"
fi

# create directory
echo -e "··············· Preparing to create the android device directory.\n"
if [ ! -d $DEVICE_ROOT_PATH/$VENDOR_NAME ]; then
    echo -e "··············· $DEVICE_ROOT_PATH/$VENDOR_NAME does not exist, will be create it.\n"
    mkdir -p "$DEVICE_ROOT_PATH/$VENDOR_NAME"
    echo -e "··············· Done!\n"
fi

echo -e "··············· Preparing to create the android kernel directory.\n"
if [ ! -d $KERNEL_ROOT_PATH/$VENDOR_NAME ]; then
    echo -e "··············· $KERNEL_ROOT_PATH/$VENDOR_NAME does not exist, will be create it.\n"
    mkdir -p "$KERNEL_ROOT_PATH/$VENDOR_NAME"
    echo -e "··············· Done!\n"
fi

echo -e "··············· Preparing to create the android vendor directory.\n"
if [ ! -d $VENDOR_ROOT_PATH/$VENDOR_NAME ]; then
    echo -e "··············· $VENDOR_ROOT_PATH/$VENDOR_NAME does not exist, will be create it.\n"
    mkdir -p "$VENDOR_ROOT_PATH/$VENDOR_NAME"
    echo -e "··············· Done!\n"
fi

# Pull code
echo -e "··············· Preparing to pull the code.\n"
## android device
echo -e "··············· Preparing to pull the android device code.\n"
cd $DEVICE_ROOT_PATH/$VENDOR_NAME

git clone -b $ANDROID_DEVICE_SONY_POPLAR_DSDS_BRANCH_NAME $ANDROID_DEVICE_SONY_POPLAR_DSDS_URL $ANDROID_DEVICE_SONY_POPLAR_DSDS_TRULY_PROJECT_NAME
echo -e "··············· Done!\n"

git clone -b $ANDROID_DEVICE_SONY_YOSHINO_COMMON_BRANCH_NAME $ANDROID_DEVICE_SONY_YOSHINO_COMMON_URL $ANDROID_DEVICE_SONY_YOSHINO_COMMON_TRULY_PROJECT_NAME
echo -e "··············· Done!\n"

# android kernel
echo -e "··············· Preparing to pull the android kernel code.\n"
cd $KERNEL_ROOT_PATH/$VENDOR_NAME

git clone -b $ANDROID_KERNEL_SONY_MSM8998_BRANCH_NAME $ANDROID_KERNEL_SONY_MSM8998_URL $ANDROID_KERNEL_SONY_MSM8998_TRULY_PROJECT_NAME
echo -e "··············· Done!\n"

# android vendor
echo -e "··············· Preparing to pull the android vendor code.\n"
cd $VENDOR_ROOT_PATH/$VENDOR_NAME

git clone -b $ANDROID_VENDOR_SONY_POPLAR_DSDS_BRANCH_NAME $ANDROID_VENDOR_SONY_POPLAR_DSDS_URL $ANDROID_VENDOR_SONY_POPLAR_DSDS_TRULY_PROJECT_NAME
echo -e "··············· Done!\n"

git clone -b $ANDROID_VENDOR_SONY_YOSHINO_COMMON_BRANCH_NAME $ANDROID_VENDOR_SONY_YOSHINO_COMMON_URL $ANDROID_VENDOR_SONY_YOSHINO_COMMON_TRULY_PROJECT_NAME
echo -e "··············· Done!\n"

echo -e "··············· All Done √ \n"